package com.woniuxy.cq.phrase3.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniuxy.cq.phrase3.common.DateUtil;
import com.woniuxy.cq.phrase3.common.Result;
import com.woniuxy.cq.phrase3.common.ValidationUtil;
import com.woniuxy.cq.phrase3.controller.form.InsertSxForm;
import com.woniuxy.cq.phrase3.entity.Project;
import com.woniuxy.cq.phrase3.entity.UserBm;
import com.woniuxy.cq.phrase3.entity.UserProject;
import com.woniuxy.cq.phrase3.service.IProjectService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author fanlang
 * description:
 * path: phrase3-com.woniuxy.cq.phrase3.controller.fl-ProjectController
 * date: 2020/9/7 19:15
 * version: 1.0
 */
@RestController
@Slf4j
public class ProjectController {

    @Autowired
    private IProjectService iProjectService;

    /**
     * 根据工作人员添加事项
     */
    @GetMapping("/insertSxByUser")
    public Result insertSxByUser(@Valid InsertSxForm insertSxForm , BindingResult result){
        if(result.hasErrors()){
            String validationResult = ValidationUtil.extract(result);
            log.info("校验失败:{}",validationResult);
            return Result.fail("ILLEGAL_ARGUMENTS",validationResult);
        }
        log.info("员工名称:{},项目名称:{}",insertSxForm.getUserNames(),insertSxForm.getProjectId());
        String[] u = insertSxForm.getUserNames().split(",");
        for (int i = 0; i <u.length ; i++) {
            Integer userId = iProjectService.selectIdByUserName(u[i]);
            Integer bmId = iProjectService.selectBmByuserName(u[i]);
            iProjectService.insertSxByUser(bmId,insertSxForm.getProjectId(),userId);
        }
        iProjectService.successAccept(insertSxForm.getProjectId(),u.length);
        return Result.success();
    }

    /**
     * 查询所有的部门和下面的员工
     */
    @GetMapping("/selectAllByUserBm")
    public Result selectAllByUserBm(){
        log.info("查询部门和员工");
        List<UserBm> userBms = iProjectService.selectAllByUserBm();
        return Result.success(userBms);
    }

    /**
     * 查询需要受理的项目
     */
    @RequestMapping("/acceptedItems")
    public Result acceptedItems(@RequestParam(defaultValue = "1") int pageIndex, @RequestParam(defaultValue = "5") int pageSize, HttpSession session, Integer checkStatusId) {
        log.info("Id:{}", checkStatusId);
        IPage<UserProject> items = iProjectService.acceptedItems(pageIndex, pageSize,checkStatusId);
        return Result.success(items);
    }
    /**
     * 修改项目状态
     */
    @GetMapping("/updateProjectStatus")
    public Result updateProjectStatus(Integer projectId,String projectStatus){
        log.info("id:{}在:{}修改为{}",projectId,DateUtil.getCurrentTime(),projectStatus);
        iProjectService.updateProjectStatus(projectId,projectStatus);
        return Result.success();
    }
    @GetMapping("/outAcceptRange")
    public Result outAcceptRange(Integer projectId){
        log.info("id:{}在:{}修改为不属于受理范围",projectId, DateUtil.getCurrentTime());
        iProjectService.outAcceptRange(projectId);
        return Result.success();
    }
    /**
     * 修改项目状态为 资料不齐全
     */
    @GetMapping("/incompleteData")
    public Result incompleteData(Integer projectId){
        log.info("id:{}在:{}修改为资料不齐全",projectId, DateUtil.getCurrentTime());
        iProjectService.incompleteData(projectId);
        return Result.success();
    }
}
